package com.zhangyu.shadow.repair.delegate.history.detail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.zhangyu.shadow.repair.R;
import com.zhangyu.shadow.repair.R2;
import com.zhangyu.shadow.repair.delegate.bean.HistoryDataBean;
import com.zhangyu.shadow.repair.delegate.bean.HistoryMaintainBean;
import com.zhangyu.shadow.repair.delegate.bean.HistoryTroubleBean;
import com.zhangyu.shadow.repair.delegate.bean.PersonBean;
import com.zhangyu.shadow.repair.delegate.bean.TempWorkBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import st.lib.base.delegate.mvp.MvpDelegate;
import st.lib.base.ui.recycler.BaseDecoration;
import st.lib.base.ui.recycler.RecyclerViewNoBugLinearLayoutManager;
import st.lib.base.util.utilcode.util.ScreenUtils;

/**
 * @author zhangyu
 * @date 2018/12/5
 */

public class HistoryDetailDelegate extends MvpDelegate<HistoryDetailPresenter>
        implements HistoryDetailContract.View {
    @BindView(R2.id.toolbar)
    Toolbar mToolbar;
    @BindView(R2.id.tv_bus_number)
    TextView mTvBusNumber;
    @BindView(R2.id.tv_in_factory_time)
    TextView mTvInFactoryTime;
    @BindView(R2.id.tv_report_content)
    TextView mTvReportContent;
    @BindView(R2.id.tv_report_name)
    TextView mTvReportName;
    @BindView(R2.id.recycler_view_repair_content)
    RecyclerView mRecyclerViewRepairContent;

    public static HistoryDetailDelegate create(String id, String type) {
        HistoryDetailDelegate detailDelegate = new HistoryDetailDelegate();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        bundle.putString("type", type);
        detailDelegate.setArguments(bundle);
        return detailDelegate;
    }

    private String mId;
    private String mType;

    @Override
    public Object setLayout() {
        return R.layout.delegate_history_data;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        mToolbar.setTitle("历史详情");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
        mRecyclerViewRepairContent.setLayoutManager(new RecyclerViewNoBugLinearLayoutManager(_mActivity));
        mRecyclerViewRepairContent.addItemDecoration(BaseDecoration.create(_mActivity.getResources()
                .getColor(R.color.color_white), 1));
        Bundle bundle = getArguments();
        if (bundle != null) {
            mId = bundle.getString("id");
            mType = bundle.getString("type");
        }
        mPresenter.getHistoryData(mId, mType);
    }

    @Override
    public void showLoading(String str) {

    }

    @Override
    public void hideLoading() {

    }


    @Override
    public HistoryDetailPresenter getPresenter() {
        return new HistoryDetailPresenter(this);
    }

    @Override
    public void showHistoryList(HistoryDataBean bean) {
        if (bean != null) {
            mTvBusNumber.setText(String.format("车辆编号:%s  线路:%s  %s", bean.getVehicleCode(),
                    bean.getRouteName(), bean.getTypeName()));
            List<PersonBean> confirmPersons = bean.getConfirmPerson();
            String confirmPerson = "";
            if (confirmPersons != null && !confirmPersons.isEmpty()) {
                confirmPerson = "过检:" + confirmPersons.get(0).getName();
            }
            List<PersonBean> completePersons = bean.getCompletedPerson();
            String completePerson = "";
            if (completePersons != null && !completePersons.isEmpty()) {
                completePerson = "竣检:" + completePersons.get(0).getName();
            }
            if (ScreenUtils.isPad(_mActivity)) {
                mTvInFactoryTime.setText(String.format("进厂时间:%s   开工时间:%s   完工时间:%s   出厂时间:%s   %s%s",
                        bean.getInFactoryTime(), bean.getActualStartTime(),
                        bean.getActualEndTime(), bean.getOutFactoryTime(),
                        confirmPerson, completePerson));
            } else {
                mTvInFactoryTime.setText(String.format("进厂时间:%s\n开工时间:%s\n完工时间:%s\n出厂时间:%s\n%s%s",
                        bean.getInFactoryTime(), bean.getActualStartTime(),
                        bean.getActualEndTime(), bean.getOutFactoryTime(),
                        confirmPerson, completePerson));
            }
            mTvReportContent.setText(bean.getReportContent());
            List<TempWorkBean> tempWorkBeans = new ArrayList<>();
            List<HistoryMaintainBean> maintainBeans = bean.getMaintenanceWorkContent();
            if (maintainBeans != null) {
                for (HistoryMaintainBean maintainBean : maintainBeans) {
                    tempWorkBeans.add(new TempWorkBean(maintainBean.getClassificationName(),
                            maintainBean.getSortId(), maintainBean.getWrokPerosnList()));
                }
            }
            List<HistoryTroubleBean> troubleBeans = bean.getWorkContentList();
            if (troubleBeans != null) {
                for (HistoryTroubleBean troubleBean : troubleBeans) {
                    tempWorkBeans.add(new TempWorkBean(troubleBean.getTroubleTypeName(),
                            troubleBean.getWorkContent(), troubleBean.getWrokPerosnList()));
                }
            }
            mRecyclerViewRepairContent.setAdapter(new HistoryWorkAdapter(R.layout.item_history_work,
                    tempWorkBeans));
        }
    }

    @Override
    public boolean onBackPressedSupport() {
        if (!isFastClick()) {
            pop();
        }
        return true;
    }

}
